
public class Truck extends Thread{

	public static long time = Scarecrow_Factory.time;
	public int truckId = 0;	
	public int deliveryAttempts = 0;
	public String partType;
	public boolean partIsSurplus = false;
	public boolean truckReturned = false;
	private static int truckCount = 0;
	
	public Truck() {
		truckCount++;
		truckId = truckCount;
		setName("Truck- T" + truckCount);
	}

	public void msg(String m) {
		System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);
	}
	
	public void run(){
		msg("A Truck Thread has Started Running (truck #"+ truckId + ")");
		speedUp();
		deliveryAttempts++;
		Deliver();
		if (!truckReturned)
			yield();
	}
	
	public void speedUp() {
		int higherPriority;
		if ((this.getPriority()+3)>10)
			higherPriority = 10;
		else higherPriority = this.getPriority()+3;
		
		this.setPriority(higherPriority);		
	}

	public void Deliver() {
		this.setPriority(Thread.NORM_PRIORITY);
		msg("Truck #" + truckId + " Has arrived at factory with a "+partType);
		Factory.truckQue.addElement(this);		
	}
	
	public boolean is2ndTry(){
		if(deliveryAttempts > 1)
			return true;
		else return false;
	}
	
	public String getPartType (){
		return partType;
	}
	
	public void setPartType(String p){
		partType = p;
	}
	
	public boolean partIsSurplus(){
		return partIsSurplus;
	}

	public String toString(){
		return("Truck #"+truckId+"; Delivery Attempt = "+deliveryAttempts+"; Part = " +partType);
	}
}
